package org.gdstash.quest;

import org.gdstash.file.GDReader;
import org.gdstash.util.FileVersionException;
import org.gdstash.util.GDMsgFormatter;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

public class GDQuestTokenList {
    private static final int BLOCK = 10;
    private int version;
    private List<String> tokens;

    public GDQuestTokenList() {
        this.tokens = new LinkedList<String>();
    }

    public void read() throws IOException {
        int val = 0;
        final GDReader.Block block = new GDReader.Block();
        val = GDReader.readBlockStart(block);
        if (val != 10) {
            throw new FileVersionException(GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_UNSUPPORTED_VERSION"));
        }
        this.version = GDReader.readEncInt(true);
        if (this.version != 2) {
            throw new FileVersionException(GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_UNSUPPORTED_VERSION"));
        }
        this.tokens.clear();
        val = GDReader.readEncInt(true);
        for (int i = 0; i < val; ++i) {
            final String token = GDReader.readEncString();
            this.tokens.add(token);
        }
        GDReader.readBlockEnd(block);
    }

    public void listTokens() {
        for (final String s : this.tokens) {
            System.out.println(s);
        }
    }
}
